#pragma once
#include<iostream>
#include<string>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<sys/time.h>
#include<atomic>
#include<fstream>
#include<vector>
#include<boost/algorithm/string.hpp>
namespace ns_util
{
    const std::string temp_path="./temp/";
    class  TimeUtil
    {
        public:
        //获取当前时间戳
        static std::string GetTimeStamp()
        {
            struct timeval _time;
            gettimeofday(&_time,nullptr);
            return std::to_string(_time.tv_sec);
        }
        //获取毫秒级时间戳
        static std::string GetTimeMs()
        {
            struct timeval _time;
            return std::to_string(_time.tv_sec*1000+_time.tv_usec/1000);
        }
    };

    class PathUtil
    {
        public:
        //编译时需要的临时文件；
        //添加后缀
        static std::string AddSuffix(const std::string &file_name,const std::string &suffix)
        {
            std::string path_name=temp_path;
            path_name+=file_name;
            path_name+=suffix;
            return path_name;
        }

        //构建源文件路径+后缀的完整文件名
        //1111->/temp/1111.cpp
        static std::string Src(const std::string &file_name)
        {
            return AddSuffix(file_name,".cpp");
        }

        //构建可执行程序的完整路径+后缀名
         //1111->/temp/1111.exe
        static std::string Exe(const std::string &file_name)
        {
            return AddSuffix(file_name,".exe");
        }

        static std::string CompilerError(const std::string &file_name)
        {
            return AddSuffix(file_name,".compile_error");
        }


        //运行时需要的临时文件；
        //构建该程序对应的标准错误完整的路径+后缀名
        static std::string Stdin(const std::string &file_name)
        {
            return AddSuffix(file_name,".stdin");
        }
        static std::string Stdout(const std::string &file_name)
        {
            return AddSuffix(file_name,".stdout");
        }
        //1111->/temp/1111.stderr
        static std::string Stderr(const std::string &file_name)
        {
            return AddSuffix(file_name,".stderr");
        }
        
    };

    class FileUtil{
        public:
        static bool IsFileExists(const std::string &path_name)
        {
            struct stat st;
            if(stat(path_name.c_str(),&st)== 0)
            {
                //获取属性成功，文件已经存在
                return true;
            }
            return false;
        }
        //生成独特文件名
        static std::string UniqFilename()
        {
            static std::atomic_uint id(0);
            id++;
            std::string ms=TimeUtil::GetTimeMs();
            std::string uniq_id=std::to_string(id);
            return ms+uniq_id;
        }
        //像文件中写入数据
        static bool WriteFile(const std::string &target,const std::string& content)
        {
            std::ofstream out(target);
            if(!out.is_open())
            {
                return false;
            }
            out.write(content.c_str(),content.size());
            out.close();
            return true;
        }
        //从文件中读取数据
        static bool ReadFile(const std::string &target,std::string *content,bool key=false)
        {
            (*content).clear();
            std::ifstream in(target);
            if(!in.is_open())
            {
                return false;
            }
            std::string line;
            //getline不保存\n，特定需求特殊处理
            while(std::getline(in,line))
            {
                (*content)+=line;
                (*content)+=(key?"\n":"");
            }
            in.close();
            return true;
        }
    };

    class StringUtill
    {
    public:
        static void SplitSpring(const std::string &line,std::vector<std::string>*tokens,std::string otp)
        {
            boost::split((*tokens),line,boost::is_any_of(otp),boost::algorithm::token_compress_on);
            //boost::split()
        }
    };
    
}
